package com.villaday.idf.custom;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter2.ChooseAddrAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.model2.Districts;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 地址选择
 */
public class ChooseAddrDialog extends BaseDialog implements View.OnClickListener, OkHttpUtil.OnDataListener {
    private String code;
    private Gson gson = new GsonBuilder().create();
    private int currentLevel = 0;//当前显示的等级
    private ChooseAddrAdapter chooseAddrAdapter0;
    private ChooseAddrAdapter chooseAddrAdapter1;
    private ChooseAddrAdapter chooseAddrAdapter2;
    private LoadingDialog loadingDialog;

    private List<Districts.BodyEntity> data0 = new ArrayList<>();
    private List<Districts.BodyEntity> data1 = new ArrayList<>();
    private List<Districts.BodyEntity> data2 = new ArrayList<>();
    private String name0;
    private String name2;
    private String name1;
    private TextView tvTitle;

    public static ChooseAddrDialog newInstance() {
        ChooseAddrDialog dialog = new ChooseAddrDialog();
        Bundle bundle = new Bundle();

        dialog.setArguments(bundle);
        return dialog;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        //取消标题显示
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getDialog().getWindow();

        View view = inflater.inflate(R.layout.dialog_choose_addr,
                ((ViewGroup) window.findViewById(android.R.id.content)), false);
        //必须设置，否则不能全屏
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.BOTTOM);

        //点击对话框外不可取消
        getDialog().setCanceledOnTouchOutside(false);
        //进入退出动画
        window.setWindowAnimations(R.style.animTranslateBottom);

        tvTitle = (TextView) view.findViewById(R.id.tv_addr);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancle);
        TextView tvConfirm = (TextView) view.findViewById(R.id.tv_confirm);

        RecyclerView rv0 = (RecyclerView) view.findViewById(R.id.rv_0);
        RecyclerView rv1 = (RecyclerView) view.findViewById(R.id.rv_1);
        RecyclerView rv2 = (RecyclerView) view.findViewById(R.id.rv_2);
        rv0.setLayoutManager(new LinearLayoutManager(getContext()));
        rv1.setLayoutManager(new LinearLayoutManager(getContext()));
        rv2.setLayoutManager(new LinearLayoutManager(getContext()));
        chooseAddrAdapter0 = new ChooseAddrAdapter(getContext());
        chooseAddrAdapter1 = new ChooseAddrAdapter(getContext());
        chooseAddrAdapter2 = new ChooseAddrAdapter(getContext());

        rv0.setAdapter(chooseAddrAdapter0);
        rv1.setAdapter(chooseAddrAdapter1);
        rv2.setAdapter(chooseAddrAdapter2);
        chooseAddrAdapter0.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                currentLevel = 1;
                name0 = data0.get(position).getName();
                tvTitle.setText(name0 + "");
                code = data0.get(position).getCode();
                for (int i = 0; i < data0.size(); i++) {
                    data0.get(i).setCheck(false);
                }
                data0.get(position).setCheck(true);
                chooseAddrAdapter0.notifyDataSetChanged();
                data2.clear();
                chooseAddrAdapter2.notifyDataSetChanged();
                getaddr();

            }
        });
        chooseAddrAdapter1.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {


            @Override
            public void onItemClick(View v, int position) {
                currentLevel = 2;
                name1 = data1.get(position).getName();
                tvTitle.setText(name0 + "/" + name1 + "");
                code = data1.get(position).getCode();
                for (int i = 0; i < data1.size(); i++) {
                    data1.get(i).setCheck(false);
                }
                data1.get(position).setCheck(true);
                chooseAddrAdapter1.notifyDataSetChanged();
                data2.clear();
                chooseAddrAdapter2.notifyDataSetChanged();
                getaddr();

            }
        });
        chooseAddrAdapter2.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                name2 = data2.get(position).getName();
                tvTitle.setText(name0 + "/" + name1 + "/" + name2);
                code = data2.get(position).getCode();
                for (int i = 0; i < data2.size(); i++) {
                    data2.get(i).setCheck(false);
                }
                data2.get(position).setCheck(true);
                chooseAddrAdapter2.notifyDataSetChanged();
            }
        });


        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);


        getaddr();

        return view;
    }



    @Override
    public void onClick(View v) {
        v.setTag(R.id.tag_first,tvTitle.getText().toString());
        v.setTag(R.id.tag_second,code);

        onItemClickListener.onItemClick(v);
        dismiss();
    }

    @TargetApi(Build.VERSION_CODES.M)
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void getaddr() {
        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(getFragmentManager());
        HashMap<String, String> map = new HashMap<>();
        map.put("code", code);
        OkHttpUtil.getJSON(getContext(), Constant.URL.districts, map, this);
    }

    @Override
    public void onResponse(String url, String json) {
        dismissLoading();
        if (!TextUtils.isEmpty(json)) {
            LogeUtil.e("districts", json);
            Districts districts = gson.fromJson(json, Districts.class);
            if (districts != null && "200".equals(districts.getCode())) {
                if (currentLevel == 0) {//一级
                    data0.clear();
                    data0.addAll(districts.getBody());
                    chooseAddrAdapter0.setData(data0);
                } else if (currentLevel == 1) {//一级
                    data1.clear();
                    data1.addAll(districts.getBody());
                    chooseAddrAdapter1.setData(data1);
                } else if (currentLevel == 2) {//二级
                    data2.clear();
                    data2.addAll(districts.getBody());
                    chooseAddrAdapter2.setData(data2);
                }
            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {

        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }

    }
}
